package com.adgox.tiantianbiting.redpacket.rank_listen;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.adapter.RankingListenAdapter;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.PageBean;
import com.adgox.tiantianbiting.bean.RankListenBean;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RankingListenActivity extends AppCompatActivity {

    @BindView(R.id.rv)
    RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking_listen);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        DialogUtils.showLoadingDialog(this);
        OkGo.<BaseResponseBean<PageBean<RankListenBean>>>get(ApiConstant.BASE_URL+ApiConstant.RANKING_LISTEN)
                .params("startPoint",1)
                .params("maxPage",50)
                .execute(new JsonCallback<BaseResponseBean<PageBean<RankListenBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<PageBean<RankListenBean>>> response) {
                        DialogUtils.dismiss();
                        if (!"0".equals(response.body().getCode())||response.body().getData() == null){
                            Toast.makeText(RankingListenActivity.this, "获取排行榜数据失败", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        initRecyclerview(response.body().getData().getData());
                    }
                });
    }

    private void initRecyclerview(List<RankListenBean> data) {
        LinearLayoutManager lm = new LinearLayoutManager(this);
        rv.setLayoutManager(lm);
        RankingListenAdapter adapter = new RankingListenAdapter(data);
        rv.setAdapter(adapter);
    }

    @OnClick(R.id.img_finish)
    public void onFinishClicked() {
        finish();
    }
}
